﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DBProvider.Providers
{
	public class QQIDProvider : IQQIDProvider
	{
		#region IQQIDProvider Members

		public QQID CreateQQID(QQID theNewQQID, out string msg)
		{
			if (theNewQQID == null || string.IsNullOrEmpty(theNewQQID.QQID1))
			{
				throw new ArgumentNullException();
			}

			QQID result = null;
			msg = string.Empty;

			try
			{
				using (DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var isExist = db.QQIDs.Count(q => q.QQID1 == theNewQQID.QQID1) > 0;

					if (isExist)
					{
						msg = "qq号已存在";
					}
					else
					{
						db.QQIDs.InsertOnSubmit(theNewQQID);
						db.SubmitChanges();

						result = theNewQQID;
					}
				}
			}
			catch (Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public bool DeleteQQID(string qqid)
		{
			if (string.IsNullOrEmpty(qqid))
			{
				throw new ArgumentException();
			}

			var result = false;

			try
			{
				using (DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var dbQQID = db.QQIDs.SingleOrDefault(q => q.QQID1 == qqid);

					if (dbQQID != null)
					{
						db.QQIDs.DeleteOnSubmit(dbQQID);
						db.SubmitChanges();

						result = true;
					}
				}
			}
			catch (Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public bool UpdateQQID(QQID theQQ)
		{
			if (theQQ == null)
			{
				throw new ArgumentNullException();
			}

			var result = false;

			try
			{
				using (DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var dbQQID = db.QQIDs.SingleOrDefault(q => q.QQID1 == theQQ.QQID1);

					if (dbQQID != null)
					{
						dbQQID.Clone(theQQ);

						db.SubmitChanges();

						result = true;
					}
				}
			}
			catch (Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public QQID GetQQID(string qqid)
		{
			if (string.IsNullOrEmpty(qqid))
			{
				throw new ArgumentException();
			}

			QQID result = null;

			try
			{
				using (DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					result = db.QQIDs.SingleOrDefault(q => q.QQID1 == qqid);
				}
			}
			catch (Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public bool IsExist(string qqid)
		{
			if(string.IsNullOrEmpty(qqid))
			{
				throw new ArgumentException();
			}

			var result = false;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					result = db.QQIDs.Count(q => q.QQID1 == qqid) > 0;
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		#endregion
	}
}
